'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx, app } = this
    this.ctx.body = 'Hello world';

    if (this.ctx.cookies.get('token')) {
      this.app.jwt.verify(this.ctx.cookies.get('token'), this.app.config.jwt.secret, function(err, decoded) {
        console.log('err', err.message)
        console.log('解密',decoded) // bar
      });
      console.log('获取', this.ctx.cookies.get('token'))
    } else {
      const token = this.app.jwt.sign({ foo: 'bar' }, this.app.config.jwt.secret, { expiresIn: 1 * 60 });
      this.ctx.cookies.set('token', token)
      console.log('设置', token)
    }
    
    // const { id } = this.ctx.body
    // const result = await this.ctx.service.select(id)
    // this.ctx.body = {
    //   code: 1,
    //   msg: result
    // }
  }
}

module.exports = HomeController;